<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            margin: 0;
        }
        .box {
            height: 100px;
            width: 100px;
            background-color: red;
            transform: translate(0, 0);
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <script>

        const box = document.querySelector('.box');
        
        let isMouseDown = false;
        let innerX = 0;
        let innerY = 0;

        box.addEventListener('mousedown', (e) => {
            innerX = e.offsetX;
            innerY = e.offsetY;
            isMouseDown = true;
        });

        box.addEventListener('mouseup', () => {
            isMouseDown = false;
        });

        document.addEventListener('mousemove', (e) => {
            if (!isMouseDown) {
                return;
            }
            const x = e.clientX;
            const y = e.clientY;
            box.setAttribute('style', `transform: translate(${ x - innerX }px, ${ y - innerY }px)`);
        });

    </script>
</body>
</html>